<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Model\Es;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class EsController extends Controller
{
    public function __construct()
    {
        $this->es = new Es();
    }

    public function selectIndex()
    {
        $this->es->selectIndex();

    }

    public function searchDoc(Request $request)
    {
        $input = $request->input();
        #数据验证
        $validator = Validator::make($input, [
            'keyword' => 'required',
        ], [
            'keyword.required' => '关键词不能为空'
        ]);
        if ($validator->fails()) {
            foreach ($validator->errors()->all() as $message) {
                returnJson(-2003, $message);
            }
        }
        $data = $this->es->searchDoc($input['keyword']);
        returnJson(200, 'success', $data);
    }

    /**
     * 创建文档
     * @param Request $request
     */
    public function createDoc(Request $request)
    {
        $input = $request->input();
        #数据验证
        $validator = Validator::make($input, [
            'desc' => 'required',
        ], [
            'desc.required' => 'desc不能为空'
        ]);
        if ($validator->fails()) {
            foreach ($validator->errors()->all() as $message) {
                returnJson(-2003, $message);
            }
        }
        $result = $this->es->createDoc($input['desc']);
        if ($result['result'] == 'created') {
            returnJson(200, 'success', $result);
        } else {
            returnJson(-2004, 'error', $result);
        }

    }

    /**
     * 删除索引
     * @param Request $request
     */
    public function deleteIndex(Request $request)
    {
        $input = $request->input();
        #数据验证
        $validator = Validator::make($input, [
            'index' => 'required',
        ], [
            'index.required' => '索引不能为空'
        ]);
        if ($validator->fails()) {
            foreach ($validator->errors()->all() as $message) {
                returnJson(-2003, $message);
            }
        }
        $index = $input['index'];
        #判断索引是否存在,进行查询操作
        $bool = $this->es->isExist($index);
        if ($bool == false) {
            returnJson(-2004, '索引不存在');
        }
        # 删除指定索引
        $dataIndex = $this->es->deleteIndex($index);
        if ($dataIndex['acknowledged'] == true) {
            returnJson(200, 'success', $dataIndex);
        } else {
            returnJson(-2004, 'error', $dataIndex);
        }
    }

    /**
     * 创建索引
     */
    public function createIndex(Request $request)
    {
        $index = env('ES_INDEX');
        $bool = $this->es->isExist($index);
        if ($bool) {
            returnJson(500, '索引'.$index.'已存在');
        }
        $dataIndex = $this->es->createIndex($index);
        if ($dataIndex['acknowledged'] == true && $dataIndex['shards_acknowledged'] == true) {
            returnJson(200, 'success', $dataIndex);
        } else {
            returnJson(-2004, 'error', $dataIndex);
        }
    }
}
